#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import os 
from threading import Thread
from Queue import Queue

import rss

FEEDS = []

def queue_worker(q):
    while True:
        url = q.get()
        dom = rss.read_source(url)
        if dom:
            FEEDS.append(dom)

        q.task_done()

def main(argv):
    q = Queue()
    WORKERS = 5
    sieve = rss.RSSieve(10)
    if len(sys.argv) == 2:
        output_file = sys.argv[1]
    else:
        output_file = None

    sources = rss.feed_sources()
    if len(sources) < WORKERS:
        WORKERS = len(sources)

    for u in range(WORKERS):
        t = Thread(target=queue_worker, args=(q,))
        t.setDaemon(True)
        t.start()

    for s in sources:
        q.put(s)
    
    q.join()

    for r in FEEDS:
        f = rss.Feed(r)
        items = f.items()
        sieve.add_feed(items)

    if output_file:
        sieve.to_rss(output_file)
        os.chmod(output_file, 0750)
    else:
        print sieve.to_rss()

if __name__ == '__main__':
    main(sys.argv)

